/*
Purpose: Long-change graphs of outcome variables vs. state NAEP scores
*/

use "data/clean/math8_normed", clear

rename state_fips fips_birth
rename scale_score_norm math08sd92
gen yob = year - 13
keep fips_birth yob math08sd92
reshape wide math08sd92,i(fips_birth) j(yob)

*the test score data have gaps by yob, and are missing for some states, taking averages
egen math08_7779=rowmean(math08sd921977 math08sd921979)
egen math08_8790=rowmean(math08sd921990 math08sd921987)
egen math08_9294=rowmean(math08sd921992 math08sd921994)
egen math08_9698=rowmean(math08sd921996 math08sd921998)
egen math08_9800=rowmean(math08sd921998 math08sd922000)

gen change_77_90=math08_8790-math08_7779
gen change_77_94=math08_9294-math08_7779
gen change_77_98=math08_9698-math08_7779
gen change_77_00=math08_9800-math08_7779

sort fips_birth

keep fips_birth change_77_90 change_77_94 math08_7779 change_77_98 change_77_00
save "data/clean/change7790",replace

use "data/raw/arrests by state year and yob" //this is the arrest rate data (mostly used in other analyses)
destring l_arrs_cap_vio, replace force

gen yob=relevant_yob
gen     cohort=1  if (yob==1977|yob==1978|yob==1979)
replace cohort=2  if (yob==1980|yob==1981|yob==1982|yob==1983)
replace cohort=3  if (yob==1987|yob==1988|yob==1989|yob==1990)
replace cohort=4  if (yob==1992|yob==1993|yob==1994)
replace cohort=5  if (yob==1996|yob==1997|yob==1998)

drop if cohort==.

rename fips fips_birth

collapse (mean) l_arrs_cap_vio l_arrs_cap_pro, by(fips_birth cohort)

reshape wide l_arrs_cap_vio l_arrs_cap_pro,i(fips_birth) j(cohort)

gen change7798_vioarr = l_arrs_cap_vio5 - l_arrs_cap_vio1
gen change7798_proarr = l_arrs_cap_pro5 - l_arrs_cap_pro1

sort fips_birth
merge 1:1 fips_birth using "data/clean/change7790"

*not using dc or those born before 1987...otherwise perfect match
drop if _merge==1|_merge==2

*dropping dc
drop if fips_birth==11

label define statefips 1 AL 2 AK 4 AZ 5 AR 6 CA 8 CO 9 CT 10 DE 11 DC 12 FL 13 GA 15 HI 16 ID 17 IL 18 IN 19 IA 20 KS 21 KY 22 LA 23 ME 24 MD 25 MA 26 MI 27 MN 28 MS 29 MO 30 MT 31 NE 32 NV 33 NH 34 NJ 35 NM 36 NY 37 NC 38 ND 39 OH 40 OK 41 OR 42 PA 44 RI 45 SC 46 SD 47 TN 48 TX 49 UT 50 VT 51 VA 53 WA 54 WV 55 WI 56 WY 60 AS 66 GU 72 PR 78 VI

label values fips_birth statefips

label var change_77_98 "Change 8th Gr Math"

twoway scatter change7798_vioarr change_77_98,msize(small) mlabel(fips_birth)|| lfit change7798_vioarr change_77_98,saving("results/change_vioarr.gph",replace) legend(off)  l1title("Violent Crime Arrests")
twoway scatter change7798_proarr change_77_98,msize(small) mlabel(fips_birth)|| lfit change7798_proarr change_77_98,saving("results/change_proarr.gph",replace) legend(off)  l1title("Property Crime Arrests")

use "data/clean/tempanal", clear

preserve
gen     cohort=1  if (yob==1977|yob==1978|yob==1979)
replace cohort=2  if (yob==1980|yob==1981|yob==1982|yob==1983)
replace cohort=3  if (yob==1987|yob==1988|yob==1989|yob==1990)
replace cohort=4  if (yob==1992|yob==1993|yob==1994)
replace cohort=5  if (yob==1996|yob==1997|yob==1998)

replace lnincearn=. if age<28
replace baplus=. if age<23
replace hsplus=. if age<23
replace somecollplus=. if age<23
replace college_enroll=. if age<=18|age>24
replace teenage_mother=. if age>30|male==1
replace institutionalized=. if age>24|male==0
keep if cohort!=.

collapse (mean) lnincearn baplus hsplus somecollplus college_enroll teenage_mother institutionalized (rawsum) perwt [pw=perwt],by(fips_birth cohort)

reshape wide lnincearn baplus hsplus somecollplus college_enroll teenage_mother institutionalized perwt,i(fips_birth) j(cohort)

recode fips_birth (9 23 25 33 44 50 34 36 42 = 1 "Northeast") (17 18 26 39 55 19 20 27 29 31 38 46 = 2 "Midwest") (10 11 12 13 24 37 45 51 54 1 21 28 47 5 22 40 48 = 3 "South") (4 8 16 30 32 35 49 56 2 6 15 41 53 =4 "West"), gen(region)

gen change7790_inc=lnincearn3-lnincearn1
gen change7794_hspl=hsplus4-hsplus1
gen change7794_bapl=baplus4-baplus1
gen change7794_scpl=somecollplus4-somecollplus1
gen change7798_coll=college_enroll5-college_enroll1

sort fips_birth
merge 1:1 fips_birth using "data/clean/change7790"

*dropping dc
drop if fips_birth==11

label var change_77_90 "Change 8th Gr Math"
label var change_77_94 "Change 8th Gr Math"
label var change_77_98 "Change 8th Gr Math"

label define statefips 1 AL 2 AK 4 AZ 5 AR 6 CA 8 CO 9 CT 10 DE 11 DC 12 FL 13 GA 15 HI 16 ID 17 IL 18 IN 19 IA 20 KS 21 KY 22 LA 23 ME 24 MD 25 MA 26 MI 27 MN 28 MS 29 MO 30 MT 31 NE 32 NV 33 NH 34 NJ 35 NM 36 NY 37 NC 38 ND 39 OH 40 OK 41 OR 42 PA 44 RI 45 SC 46 SD 47 TN 48 TX 49 UT 50 VT 51 VA 53 WA 54 WV 55 WI 56 WY 60 AS 66 GU 72 PR 78 VI

label values fips_birth statefips

eststo clear
eststo eq1a: quietly reg change7790_inc change_77_90             [pw=perwt2],robust
eststo eq1b: quietly reg change7790_inc change_77_90 math08_7779 [pw=perwt2],robust
eststo eq1c: quietly reg change7790_inc change_77_90 lnincearn1  [pw=perwt2],robust
eststo eq2a: quietly reg change7794_hspl change_77_94            [pw=perwt2],robust
eststo eq2b: quietly reg change7794_hspl change_77_94 math08_7779 [pw=perwt2],robust
eststo eq2c: quietly reg change7794_hspl change_77_94 hsplus1     [pw=perwt2],robust
eststo eq3a: quietly reg change7794_scpl change_77_94            [pw=perwt2],robust
eststo eq3b: quietly reg change7794_scpl change_77_94 math08_7779 [pw=perwt2],robust
eststo eq3c: quietly reg change7794_scpl change_77_94 somecollplus1 [pw=perwt2],robust
eststo eq4a: quietly reg change7794_bapl change_77_94            [pw=perwt2],robust
eststo eq4b: quietly reg change7794_bapl change_77_94 math08_7779 [pw=perwt2],robust
eststo eq4c: quietly reg change7794_bapl change_77_94 baplus1     [pw=perwt2],robust
eststo eq5a: quietly reg change7798_coll change_77_98             [pw=perwt2],robust
eststo eq5b: quietly reg change7798_coll change_77_98 math08_7779 [pw=perwt2],robust
eststo eq5c: quietly reg change7798_coll change_77_98 college_enroll1 [pw=perwt2],robust   
esttab using "results/long change regs", csv r2 replace label



twoway scatter change7790_inc change_77_90  [pw=perwt2],msize(small) mlabel(fips_birth)|| lfit change7790_inc change_77_90  [pw=perwt2],saving("results/change_inc",replace) legend(off)  l1title("LnEarn")
twoway scatter change7794_bapl change_77_94 [pw=perwt2],msize(small) mlabel(fips_birth)|| lfit change7794_bapl change_77_94 [pw=perwt2],saving("results/change_bapl",replace) legend(off)  l1title("BA+")
twoway scatter change7794_scpl change_77_94 [pw=perwt2],msize(small) mlabel(fips_birth)|| lfit change7794_scpl change_77_94 [pw=perwt2],saving("results/change_scpl",replace) legend(off)  l1title("Some Coll+")
twoway scatter change7794_hspl change_77_94 [pw=perwt2],msize(small) mlabel(fips_birth)|| lfit change7794_hspl change_77_94 [pw=perwt2],saving("results/change_hspl",replace) legend(off)  l1title("HS+ ")
twoway scatter change7798_coll change_77_98 [pw=perwt2],msize(small) mlabel(fips_birth)|| lfit change7798_coll change_77_98 [pw=perwt2],saving("results/change_coll",replace) legend(off)  l1title("Coll Enroll")

restore

gen     cohort=1  if (yob==1977|yob==1978|yob==1979)
replace cohort=2  if (yob==1980|yob==1981|yob==1982|yob==1983)
replace cohort=3  if (yob==1987|yob==1988|yob==1989|yob==1990)
replace cohort=4  if (yob==1992|yob==1993|yob==1994)
replace cohort=5  if (yob==1998|yob==1999|yob==2000)

replace lnincearn=. if age<28
replace baplus=. if age<23
replace hsplus=. if age<23
replace somecollplus=. if age<23
replace college_enroll=. if age<=18|age>24
replace teenage_mother=. if age>30|male==1
replace institutionalized=. if age>24|male==0
keep if cohort!=.

collapse (mean) lnincearn baplus hsplus somecollplus college_enroll teenage_mother institutionalized (rawsum) perwt [pw=perwt],by(fips_birth cohort)

reshape wide lnincearn baplus hsplus somecollplus college_enroll teenage_mother institutionalized perwt,i(fips_birth) j(cohort)

recode fips_birth (9 23 25 33 44 50 34 36 42 = 1 "Northeast") (17 18 26 39 55 19 20 27 29 31 38 46 = 2 "Midwest") (10 11 12 13 24 37 45 51 54 1 21 28 47 5 22 40 48 = 3 "South") (4 8 16 30 32 35 49 56 2 6 15 41 53 =4 "West"), gen(region)

gen change7700_teenm=teenage_mother5-teenage_mother1
gen change7700_inst=institutionalized5-institutionalized1

sort fips_birth
merge 1:1 fips_birth using "data/clean/change7790"

*dropping dc
drop if fips_birth==11

label var change_77_90 "Change 8th Gr Math"
label var change_77_94 "Change 8th Gr Math"
label var change_77_00 "Change 8th Gr Math"

label define statefips 1 AL 2 AK 4 AZ 5 AR 6 CA 8 CO 9 CT 10 DE 11 DC 12 FL 13 GA 15 HI 16 ID 17 IL 18 IN 19 IA 20 KS 21 KY 22 LA 23 ME 24 MD 25 MA 26 MI 27 MN 28 MS 29 MO 30 MT 31 NE 32 NV 33 NH 34 NJ 35 NM 36 NY 37 NC 38 ND 39 OH 40 OK 41 OR 42 PA 44 RI 45 SC 46 SD 47 TN 48 TX 49 UT 50 VT 51 VA 53 WA 54 WV 55 WI 56 WY 60 AS 66 GU 72 PR 78 VI

label values fips_birth statefips


twoway scatter change7700_teenm change_77_00 [pw=perwt2],msize(small) mlabel(fips_birth)|| lfit change7700_teenm change_77_00 [pw=perwt2],saving("results/change_teenm",replace) legend(off)  l1title("Teen Mother")
twoway scatter change7700_inst change_77_00 [pw=perwt2],msize(small) mlabel(fips_birth)|| lfit change7700_inst change_77_00 [pw=perwt2],saving("results/change_inst",replace) legend(off)  l1title("Institutionalized")

graph combine "results/change_inc.gph" "results/change_hspl.gph" "results/change_coll.gph" "results/change_teenm.gph" "results/change_inst.gph" "results/change_vioarr.gph", saving("results/longchange",replace)
